import { createRouter, createWebHashHistory } from 'vue-router'

import Store from '../tools/Storage'

const routes = [
  {
    path: '/',
    level: 0,
    name: 'root',
    redirect: '/home',
  },
  {
    path: '/login',
    name: "login",
    level: 0,
    component: () => import('../login/Login.vue'),
  },
  {
    path: '/register',
    name: "register",
    level: 0,
    component: () => import('../register/Register.vue'),
  },

  {
    path: '/home',
    name: 'home',
    defaultPath: '/home/product/normal',
    level: 0,
    rootMenu: true,
    meta: {
      icon: "Menu",
      title: '遥遥领先商城',
      root: true,
      link: false
    },
    component: () => import('../views/HomeView.vue'),
    children: [
      {
        path: '/user',
        name: "user",
        level: 2,
        meta: {
          icon: "User",
          title: '个人用户信息'
        },
        component: () => import('../views/user/index.vue'),
       
      },
      {
        path: '/product',
        name: "product",
        level: 1,
        meta: {
          icon: "Shop",
          title: '商品浏览'
        },

        children: [{
          path: '/pnormal',
          name: "pnormal",
          level: 2,
          meta: {
            icon: "ShoppingCart",
            title: '我的购物车'
          },
          icon: () => import('../components/icons/IconTooling.vue'),
          component: () => import('../views/product/normal/index.vue'),
        }, {
          path: '/daily',
          name: "daily",
          level: 2,
          meta: {
            icon: "Present",
            title: '今日推荐'
          },
          component: () => import('../views/product/daily/index.vue'),
        }, {
          path: '/detail:productInfo',
          name: "detail",
          level: 0,
          meta: {
            icon: "CollectionTag",
            title: '商品详情'
          },
          component: () => import('../views/product/detail/index.vue'),
          props: true

        },
        {
          path: '/succcess:info',
          name: "succcess",
          level: 0,
          meta: {
            icon: "CollectionTag",
            title: '添加购物车'
          },
          component: () => import('../components/Succcess.vue'),
          props: true
        }
        ]
      }
      ]
  }


]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from) => {
  let isLogin = Store.getters.isLogin;
  console.log('dddddddd', from.name)
  if (isLogin || from.path === 'login' || to.name === 'login') {
    return true;
  } else {
    return { name: 'login' }

  }
})

export {
  router,
  routes
}
